#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]

use rusqlite::Connection;
mod code_info;
mod category_info;

use code_info::*;
use category_info::*;

fn create_table() {
    let conn: Connection = Connection::open("./code_snippet.db").unwrap();

    conn.execute(
        "create table if not exists code_info (
            id integer primary key autoincrement,
            title varchar(255),
            category_id integer,
            category_name varchar(255),
            code text,
            sort integer,
            gmt_create integer,
            gmt_modified integer
        )",
        (),
    )
    .unwrap();

    conn.execute(
        "create table if not exists category_info (
            id integer primary key autoincrement,
            name varchar(255),
            sort integer,
            gmt_create integer,
            gmt_modified integer
        )",
        (),
    )
    .unwrap();
}

fn main() {
    create_table();
    tauri::Builder::default()
        .plugin(tauri_plugin_window_state::Builder::new().build())
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_clipboard_manager::init())
        .invoke_handler(tauri::generate_handler![
            create_code_info,
            update_code_info,
            delete_code_info,
            view_code_info,
            list_code_info,
            list_code_info_by_category,

            create_category_info,
            update_category_info,
            delete_category_info,
            list_category_info,
            get_code_count_by_category
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
